package com.example.iterator;

/**
 * Created by Risky57 on 2017/4/28.
 */

public class Parents implements Iterable{
    private Parent p;
    private int size;
    private int position;
    public Parents () {
        p = new Parent();
        p.name = "孟爸爸";
        p.next = new Parent();
        p.next.name = "肖妈妈";
        p.next.next = new Parent();
        p.next.next.name = "武叔叔";
    }

    private int size(){
        size = 0;
        Parent current = p;
        while (current != null){
            current = current.next;
            size++;
        }
        return size;
    }

    private Parent get(int index){
        position = 0;
        Parent current = p;
        while (index != position){
            current = current.next;
            position++;
        }
        return current;
    }


    @Override
    public Iterator iterator () {
        return new LinkedIterator();
    }

    private class LinkedIterator implements Iterator{
        private int index;
        @Override
        public boolean hasNext () {
            if (index < size()) return true;
            return false;
        }

        @Override
        public String next () {
            if (hasNext()){
                return get(index++).name;
            }
            return null;
        }
    }
}
